package com.hongdee.insurance.quotationitem.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.insurance.quotationitem.entity.TInsuranceQuotationItem;
import com.hongdee.insurance.quotationitem.service.ITInsuranceQuotationItemService;
import org.aspectj.weaver.NewMemberClassTypeMunger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/27.
 */
@RestController
@RequestMapping("/insurance/QuotationItem")
public class TInsuranceQuotationItemController {
    @Autowired
    private ITInsuranceQuotationItemService itInsuranceQuotationItemService;
    /**
     * 报价单项目列表查询
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/QuotationItemList")
    @ResponseBody
    public List QuotationItemList(
                    @RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "id",defaultValue = "")String id) throws Exception {
        List QuotationItemList = itInsuranceQuotationItemService.QuotationItemList(uid,id);
        return QuotationItemList;
    }

    /**
     * 退单获得剩余未退保险项目
     * @param uid
     * @param token
     * @param qId
     * @param orderId
     * @return
     * @throws Exception
     */
    @RequestMapping("/return")
    @ResponseBody
    public ResultEntity quotationItemReturnList( @RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam(name = "qId",defaultValue = "")String qId,
                                         @RequestParam(name = "orderId",defaultValue = "")String orderId) throws Exception {
        ResultEntity res=new ResultEntity();
        List QuotationItemList = itInsuranceQuotationItemService.quotationItemReturnList(uid,qId,orderId);
        res.setSuccess(true);
        res.setDataList(QuotationItemList);
        return res;
    }

    /**
     * 根据车辆信息 查询最近一个报价单的或保单的保险项目信息
     * @param uid
     * @param token
     * @param carId
     * @param key
     * @return
     */
    @RequestMapping("/QuotationItemList/lately")
    @ResponseBody
    public List latelyQuotationItemList(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "carId",defaultValue = "")String carId,
                                        @RequestParam(name = "key",defaultValue = "")String key,
                                        @RequestParam(name = "insuranceCompanyId",defaultValue = "")String insuranceCompanyId) throws Exception {
        if("".equals(insuranceCompanyId)) {
            return itInsuranceQuotationItemService.findQuotationItemListBylately(uid, carId, key, null);
        }else{
            return itInsuranceQuotationItemService.findQuotationItemListByLateLy(uid,carId,key,insuranceCompanyId);
        }
    }

    /**
     * 新增或修改报价单
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "QuotationId",defaultValue = "")String QuotationId,
                             @RequestBody  List<Map<String,Object>> data) throws Exception {
        EntityUtils.valid(data);
        itInsuranceQuotationItemService.checkItems(data);
        this.itInsuranceQuotationItemService.saveOrUpdate(data,QuotationId,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * 所有保险项目
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/AllQuotationItemList")
    @ResponseBody
    public List AllQuotationItemList(
            @RequestParam(name = "uid", defaultValue = "") String uid,
            @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        List QuotationItemList = itInsuranceQuotationItemService.quotationList();
        return QuotationItemList;
    }
    /**
     * 已有保险项目
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/hasQuotationList")
    @ResponseBody
    public List hasQuotationList(
            @RequestParam(name = "uid", defaultValue = "") String uid,
            @RequestParam(name = "token", defaultValue = "") String token,
            @RequestParam(name = "type", defaultValue = "") String type,
            @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        List QuotationItemList = itInsuranceQuotationItemService.hasQuotationList(type,id);
        return QuotationItemList;
    }
    /**
     * 单条保险项目保存
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/singleSave")
    @ResponseBody
    public ResultEntity singleSave(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestBody TInsuranceQuotationItem data) throws Exception {
        EntityUtils.valid(data);
        this.itInsuranceQuotationItemService.singleSave(data,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * 单条保险项目删除
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/singleDel")
    @ResponseBody
    public ResultEntity singleSave(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        this.itInsuranceQuotationItemService.singleDel(id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }
}
